package com.homework.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.homework.bean.CourseBean;
import com.homework.bean.ResourceBean;
import com.homework.bean.UserBean;
import com.homework.service.ResourceService;
import com.homework.util.PublicMethod;
import com.homework.util.connDB;
import com.opensymphony.xwork2.ActionSupport;
import com.sina.sae.storage.SaeStorage;

public class ResourceUploadAction extends ActionSupport{

	private static final long serialVersionUID = 776693319724719101L;
	private ResourceService resourceService;
	private int courseId;
	private String resourceName;
	private String resourceDescribe;
	private String savePath;
	private String textfield;
	

	private File fileField;
	private String fileFieldContentType;
	private String fileFieldFileName;
//	private FileOutputStream fos;
	private FileInputStream fis;

	public ResourceUploadAction() {
		// TODO Auto-generated constructor stub
		resourceService = new ResourceService();
	}

	public int getCourseId() {
		return courseId;
	}

	public void setCourseId(int courseId) {
		this.courseId = courseId;
	}

	public File getFileField() {
		return fileField;
	}

	public void setFileField(File fileField) {
		this.fileField = fileField;
	}

	public String getFileFieldContentType() {
		return fileFieldContentType;
	}

	public void setFileFieldContentType(String fileFieldContentType) {
		this.fileFieldContentType = fileFieldContentType;
	}

	public String getFileFieldFileName() {
		return fileFieldFileName;
	}

	public void setFileFieldFileName(String fileFieldFileName) {
		this.fileFieldFileName = fileFieldFileName;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String getResourceName() {
		return resourceName;
	}

	public void setResourceName(String resourceName) {
		this.resourceName = resourceName;
	}

	public String getResourceDescribe() {
		return resourceDescribe;
	}

	public void setResourceDescribe(String resourceDescribe) {
		this.resourceDescribe = resourceDescribe;
	}

	public String getTextfield() {
		return textfield;
	}

	public void setTextfield(String textfield) {
		this.textfield = textfield;
	}

	// 资源上传
	public String resourceUpload() throws IOException, URISyntaxException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();

		savePath = "/data_homework/resource/"+getResourceName()+PublicMethod.getSuffix(getFileFieldFileName());
		
		// userBean
		UserBean userBean = new UserBean();
		userBean.setUserId(String.valueOf(session.getAttribute("userId")));
		// courseBean
		CourseBean courseBean = new CourseBean();
		courseBean.setCourseId(String.valueOf(this.getCourseId()));
		// resourceBean
		ResourceBean resourceBean = new ResourceBean();
		resourceBean.setResourceName(getResourceName());
		resourceBean.setResourceDescribe(getResourceDescribe());
		resourceBean.setAddress(getSavePath() + getResourceName()
				+ PublicMethod.getLastString(getFileFieldFileName()));

		// 将资源存放地址存到数据库
		resourceService.resourceUpload(userBean, courseBean, resourceBean);
		request.setAttribute("courseId", getCourseId());

		// 将上传的资源保存到指定位置
		SaeStorage ss = new SaeStorage(connDB.getAccessKey(),connDB.getSecretKey(),connDB.getAppName());
		fis = new FileInputStream(getFileField());
		byte[] buffer = new byte[20*1024*1024];
		int len = 0;
		len = fis.read(buffer);
		byte[] buffer2 = new byte[len];
		for (int i = 0; i < len; i++) {
			buffer2[i] = buffer[i];
		}
		ss.write("homework", savePath, buffer2);
		fis.close();
		return "Success";
	}
}
